package cn.elane.kibana.demo.core.index.controller;

import cn.elane.kibana.demo.core.index.biz.ApiBiz;
import cn.elane.kibana.demo.global.model.NameBean;
import cn.elane.kibana.demo.global.render.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/")
public class ApiController {

	@Resource
	private ApiBiz biz;

	@RequestMapping("services/list")
	public R list(
			@RequestParam("start") String start,
			@RequestParam("end") String end,
			@RequestParam("uiFilters") String uiFilters) {
		return biz.list(new NameBean().put("start", start).put("end", end).put("uiFilters", uiFilters));
	}

	@RequestMapping("traces/list")
	public R tracesList(
			@RequestParam("start") String start,
			@RequestParam("end") String end,
			@RequestParam("uiFilters") String uiFilters) {
		return biz.tracesList(new NameBean().put("start", start).put("end", end).put("uiFilters", uiFilters));
	}

	@RequestMapping("traces/listById")
	public R listById(
			@RequestParam("start") String start,
			@RequestParam("end") String end,
			@RequestParam("traceId") String traceId) {
		return biz.listById(new NameBean().put("start", start).put("end", end).put("traceId", traceId));
	}

	/**
	 * 根据时间范围获取环境列表
	 *
	 * @return
	 */
	@PostMapping("services/getEnv")
	public R getEnv(String start, String end) {
		return biz.getEnv(new NameBean().put("start", start).put("end", end));
	}


	/**
	 * 根据时间范围获取服务名称和探针条件
	 *
	 * @return
	 */
	@PostMapping("services/getParam")
	public R getParam(
			@RequestParam("start") String start,
			@RequestParam("end") String end,
			@RequestParam("uiFilters") String uiFilters,
			@RequestParam("filterNames") String filterNames) {
		return biz.getParam(new NameBean() {{
			put("start", start);
			put("end", end);
			put("uiFilters", uiFilters);
			put("filterNames", filterNames);
		}});
	}

	/**
	 * 获取原链路列表分布数据集
	 *
	 * @return
	 */
	@PostMapping("services/distribution")
	public R distribution(
			@RequestParam("start") String start,
			@RequestParam("end") String end,
			@RequestParam("uiFilters") String uiFilters,
			@RequestParam("transactionType") String transactionType,
			@RequestParam("serviceName") String serviceName,
			@RequestParam("transactionName") String transactionName) {
		return biz.distribution(start, end, uiFilters, transactionType, transactionName, serviceName);
	}

	/**
	 * 拓布图
	 *
	 * @return
	 */
	@PostMapping("services/map")
	public R serviceMap(
			@RequestParam("start") String start,
			@RequestParam("end") String end,
			@RequestParam("environment") String environment) {
		return biz.serviceMap(start, end, environment);
	}

}
